﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Drawing;
using System.Data;
using System.IO;
//using System.Windows.Media.Imaging;


public partial class client_TrendReport : @base {
    TrendReportService ts = new TrendReportService();
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            MachineNameLabel.Text = "Machine: " + MACHINENAME;
            DataTable dt = ts.getMachineMatrixByMachineID(MACHINEID);
            Session["MachineMatrix"] = dt;
            int count = dt.Rows.Count;
            // lblSiteName.Text = "Site: " + MACHINEID+" count: "+count;

            setFromDateList();
            setToDateList();

        }

    }
    protected void setFromDateList() {
        //DataTable dt = (DataTable)Session["MachineMatrix"];
        //foreach (DataRow r in dt.Rows) {
        //    String dateRecorded1 = r["date_recorded"].ToString();
        //    string dateRecorded = dateRecorded1.Trim();
        //    string dta = dateRecorded.Substring(0, dateRecorded.IndexOf(" ")).Trim();

        //    System.Web.UI.WebControls.ListItem ls = new System.Web.UI.WebControls.ListItem(dta, dateRecorded1, true);
        //    fromDate_DropDownList.Items.Add(ls);
        //}
        DataTable dt = (DataTable)Session["MachineMatrix"];
        foreach (DataRow r in dt.Rows) {
            String dateRecorded1 = r["date"].ToString();
            string dateRecorded = dateRecorded1.Trim();
            string dta = dateRecorded;//.Substring(0, dateRecorded.IndexOf(" ")).Trim();

            System.Web.UI.WebControls.ListItem ls = new System.Web.UI.WebControls.ListItem(dta, dateRecorded1, true);
            fromDate_DropDownList.Items.Add(ls);
        }




    }
    protected void setToDateList() {
        //string fromDate = fromDate_DropDownList.SelectedValue.ToString();
        //System.DateTime str_date = DateTime.Parse(fromDate, System.Globalization.CultureInfo.CreateSpecificCulture("en-AU").DateTimeFormat);
        //DataRow[] dt = ((DataTable)Session["MachineMatrix"]).Select("date_recorded > #" + str_date.ToString() + "#");
        //foreach (DataRow r in dt) {
        //    String dateRecorded1 = r["date_recorded"].ToString();
        //    string dateRecorded = dateRecorded1.Trim();
        //    string dta = dateRecorded.Substring(0, dateRecorded.IndexOf(" ")).Trim();
        //    System.Web.UI.WebControls.ListItem ls = new System.Web.UI.WebControls.ListItem(dta, dateRecorded, true);
        //    toDate_DropBoxList.Items.Add(ls);
        //}

        DataTable dtb = ((DataTable)Session["MachineMatrix"]);
        if (dtb.Rows.Count != 0) {
            int index = fromDate_DropDownList.SelectedIndex;
            int i = 0;
            foreach (DataRow r in dtb.Rows) {
                String dateRecorded1 = r["date"].ToString();
                string dateRecorded = dateRecorded1.Trim();
                string dta = dateRecorded;
                System.Web.UI.WebControls.ListItem ls = new System.Web.UI.WebControls.ListItem(dta, dateRecorded1, true);
                toDate_DropBoxList.Items.Add(ls);
                i++;
                if (i >= index)
                    break;
            }

        }


    }

    protected void fromDate_changed(object sender, EventArgs e) {
        toDate_DropBoxList.Items.Clear();
        setToDateList();
    }
    protected DataTable getMatrix() {
        //DataTable matrix = new DataTable();
        //string fromDate = fromDate_DropDownList.SelectedValue.ToString();
        //string toDate = toDate_DropBoxList.SelectedValue.ToString();

        //matrix.Columns.Add("x", typeof(string));
        //matrix.Columns.Add("y", typeof(string));
        //DataTable dt = ((DataTable)Session["MachineMatrix"]);
        //int dtnum = dt.Rows.Count;
        //// DataRow[] dates = dt.Select("date_recorded>='" + fromDate + "' ");
        //DataRow[] dates = dt.Select("date_recorded >= '" + fromDate + "' AND date_recorded <= '" + toDate + "'", "date_Recorded");
        //foreach (DataRow r in dates) {
        //    string daterecorded = r["date_recorded"].ToString();
        //    // string datenum = daterecored.Substring(daterecored.IndexOf('/')+1, 2);
        //    string matrixNo = r["matrix_no"].ToString();
        //    DataRow m = matrix.NewRow();
        //    m["x"] = daterecorded;
        //    m["y"] = matrixNo;
        //    matrix.Rows.Add(m);
        //}

        //return matrix;


        DataTable matrix = new DataTable();
        int fromDateIndex = fromDate_DropDownList.SelectedIndex;
        int toDateIndex = toDate_DropBoxList.SelectedIndex;

        matrix.Columns.Add("x", typeof(string));
        matrix.Columns.Add("y", typeof(string));
        DataTable dtb = ((DataTable)Session["MachineMatrix"]);
        int dtnum = dtb.Rows.Count;
        // string test = "num: "+dtnum+"/"+"fdate:"+fromDateIndex+"/todate:"+toDateIndex+"/";
        if (dtnum != 0) {
            for (int i = fromDateIndex; i >= toDateIndex; i--) {
                DataRow r = dtb.Rows[i];
                string daterecorded = r["date"].ToString();
                string matrixNo = r["matrix_no"].ToString();
                DataRow m = matrix.NewRow();
                m["x"] = daterecorded;
                m["y"] = matrixNo;
                matrix.Rows.Add(m);
            }
        }
        return matrix;
    }
    protected void setImageMapHotspot() {
        int intWidth = 480;
        int intHeight = 400;
        DataTable dt = (DataTable)Session["ReportValue"];
        int i = 0;
        int total = dt.Rows.Count + 1;
        foreach (DataRow r in dt.Rows) {
            string month = (r["x"].ToString());
            int matrix = ToInt(r["y"].ToString());
            i++;
            //  c.AddValue(i, matrix, month);
            CircleHotSpot Circle1 = new CircleHotSpot();
            Circle1.HotSpotMode = HotSpotMode.PostBack;
            Circle1.PostBackValue = month;
            Circle1.X = 30 + (intWidth - 2 * 30) * i / total;
            //145, 480 
            Circle1.Y = intHeight - 30 - (intHeight - 2 * 30) * matrix / 200;//120;
            Circle1.Radius = 15;
            Circle1.AlternateText = month;
            ImageMap2.HotSpots.Add(Circle1);

        }
    }
    protected void GetReport(object sender, EventArgs e) {

        DataTable dt = new DataTable();
        dt = getMatrix();
        if (dt.Rows.Count != 0) {
            Session["ReportValue"] = dt;

            //  Image1.ImageUrl = "GetTrendReportImage.aspx";
            // Image2.ImageUrl = "GetTrendReportImage.aspx";
            ImageMap2.ImageUrl = "GetTrendReportImage.aspx";
            ImageMap2.HotSpots.Clear();

            setImageMapHotspot();
            photoHyperLink.Text = "Click here to enlarge picture";
            photoHyperLink.Style["text-align"] = "center";
            photoHyperLink.Attributes.Add("href", "GetLargeTrendReportImage.aspx");
            btnNewEntry.Text = "View Picture Detail";
            btnNewEntry.Visible = true;
            btnNewEntry.Enabled = true;

        }

    }
    protected void GetValue(object sender, ImageMapEventArgs e) {
        // Response.Write("Hi," + e.ToString());
        //  TextBox1.Text = "hi" + e.PostBackValue;
        string jscript = "<script language='javascript'>alert(' " + e.PostBackValue + "')</script>";




        // Page.RegisterClientScriptBlock("keyClientBlock",jscript);
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);
        string url = "Vibration_Report.aspx?id=" + MACHINEID + "&date=" + e.PostBackValue;
        Server.Transfer(url);

    }

    //  protected void Button1_Click(object sender, EventArgs e)
    //  {
    //      //string jscript = "<script language='javascript'  type='text/javascript'> parent.document.location.href= 'GetLargeTrendReportImage.aspx'</script>";
    //      ////Page.RegisterClientScriptBlock("key2ClientBlock", jscript);
    //      //this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key3ClientBlock", jscript);

    //    /*  Session["treeview"] = 0;
    //   */
    //      string popupScript = "<script language='javascript'>" +
    //"window.open('GetLargeTrendReportImage.aspx', 'CustomPopUp', " +
    //"'width=900, height=400, menubar=yes, resizable=no')" +
    //"</script>";

    //      Page.RegisterStartupScript("PopupScript", popupScript);
    //      //Response.Redirect("GetLargeTrendReportImage.aspx");

    //  }
    protected void btnNewEntry_Click(object sender, EventArgs e) {
        //Response.Redirect("GetLargeTrendReportImage.aspx");
        string fromDate = fromDate_DropDownList.SelectedValue.ToString();
        string toDate = toDate_DropBoxList.SelectedValue.ToString();

        string popupScript = "<script language='javascript'> window.open( 'ViewLargeTrendReport.aspx?fromDate=" + fromDate + "&toDate=" + toDate + "',null,'width=900,height=500,top=250,left=250','true' ); </script>";
        //Response.Write("<script> window.open( 'GetLargeTrendReportImage.aspx' ); </script>");
        //Response.End();
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key3ClientBlock", popupScript);
    }
    //protected void hic(object sender, EventArgs e)
    //{
    //    string jscript = "<script language='javascript'  type='text/javascript'> parent.document.location.href= 'GetLargeTrendReportImage.aspx'</script>";
    //    //Page.RegisterClientScriptBlock("key2ClientBlock", jscript);
    //    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key3ClientBlock", jscript);


    //}
}
